﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Nvigorate.Relational.Mapping;
using Nvigorate.Relational.Mapping.Index;

namespace Nvigorate.Test.Relational.Binding
{
    [TestClass]
    public class MapElementMapLoaderTests : BindingTestBase
    {
        #region GetMaps Tests

        [TestMethod]
        public void GetMaps_SingleElement_ReturnsBindingMap()
        {
            // Arrange
            var elements = new[]
                               {
                                   new MapElement(),
                               };

            var bindingMapLoader = CreateBindingMapLoader("Employee");
            var bindingMapLoaderFactory = new Mock<IBindingMapLoaderFactory>();
            bindingMapLoaderFactory.Expect(f => f.GetBindingMapLoader(It.IsAny<MapElement>(), It.IsAny<Type>())).Returns
                (bindingMapLoader);

            // Act
            var loader = new MapElementMapLoader(elements, typeof (BindingMap),
                                                 bindingMapLoaderFactory.Object);
            var actual = loader.GetMaps().ToList();

            // Assert
            Assert.AreEqual(1, actual.Count);
            Assert.AreEqual("Employee", actual[0].RepositoryName);
        }

        [TestMethod]
        public void GetMaps_MultipleElements_ReturnsBindingMaps()
        {
            // Arrange
            var elements = new[]
                               {
                                   new MapElement {MapSet = "MapSetA"},
                                   new MapElement {MapSet = "MapSetB"}
                               };

            var bindingMapLoaderA = CreateBindingMapLoader("Employee");
            var bindingMapLoaderB = CreateBindingMapLoader("Customer");

            var bindingMapLoaderFactory = new Mock<IBindingMapLoaderFactory>();
            bindingMapLoaderFactory.Expect(
                f => f.GetBindingMapLoader(It.Is<MapElement>(e => e.MapSet == "MapSetA"), It.IsAny<Type>())).Returns
                (bindingMapLoaderA);
            bindingMapLoaderFactory.Expect(
                f => f.GetBindingMapLoader(It.Is<MapElement>(e => e.MapSet == "MapSetB"), It.IsAny<Type>())).Returns
                (bindingMapLoaderB);

            // Act
            var loader = new MapElementMapLoader(elements, typeof (BindingMap),
                                                 bindingMapLoaderFactory.Object);
            var actual = loader.GetMaps().ToList();

            // Assert
            Assert.AreEqual(2, actual.Count);
            Assert.IsNotNull(actual.FirstOrDefault(m => m.RepositoryName == "Employee"));
            Assert.IsNotNull(actual.FirstOrDefault(m => m.RepositoryName == "Customer"));
        }

        #endregion
    }
}